package com.qunar.mina.app.handler;

import java.util.List;

import org.apache.lucene.index.Term;

import com.qunar.mina.app.service.SearcherService;
import com.qunar.mina.app.service.impl.SearcherServiceBean;
import com.qunar.qcf.handler.InvokeHandler;
import com.qunar.qcf.handler.annotation.Handler;
import com.qunar.qcf.handler.annotation.Remote;
import com.qunar.wireless.ugc.qcfclient.dto.Address;
import com.qunar.wireless.ugc.qcfclient.dto.Query;
import com.qunar.wireless.ugc.qcfclient.dto.QueryResult;

@Handler(name = "addresshandler")
public class SearcherHandler implements InvokeHandler {

	private SearcherService searcherService = new SearcherServiceBean();

	@Remote(url = "createindex")
	public String createIndex() {
		searcherService.createIndex();
		return "create index success";
	}

	@Remote(url = "getquery")
	public QueryResult getQueryResult(Query query) {
		QueryResult qr = searcherService.searcher(query.getKeyword(), query.getFirst(), query.getMax(), "address",
				query.getFields());
		
		return qr;
	}
	
	@Remote(url = "getpic")
	public QueryResult getPicResult(Query query) {
		QueryResult qr = searcherService.searcher(query.getKeyword(), query.getFirst(), query.getMax(), "pic",
				query.getFields());
		return qr;
	}
	
	@Remote(url = "getuser")
	public QueryResult getUserResult(Query query) {
		QueryResult qr = searcherService.searcherScan(query.getKeyword(), query.getFirst(), query.getMax(), "user",
				query.getFields());
		return qr;
	}
	
	@Remote(url = "fuzzyuser")
	public QueryResult getFuzzyUser(Query query) {
		QueryResult qr = searcherService.fuzzySearcher(query.getKeyword(), query.getFirst(), query.getMax(), query.getFields()[0]);
		return qr;
	}
	
	@Remote(url = "wildcarduser")
	public QueryResult getWildCardUser(Query query) {
		QueryResult qr = searcherService.wildcardSearcher(query.getKeyword(), query.getFirst(), query.getMax(), query.getFields()[0]);
		return qr;
	}
	
	@Remote(url = "getauditor")
	public QueryResult getAuditorResult(Query query) {
		QueryResult qr = searcherService.searcherScan(query.getKeyword(), query.getFirst(), query.getMax(), "auditor",
				query.getFields());
		return qr;
	}
	
	@Remote(url = "insertaddindex")
	public String addIndex(Address address) {
		searcherService.addIndex(address, "address");
		return "add document index success";
	}
	
	@Remote(url = "deladdindex")
	public String deleteAddIndex(Term term) {
		searcherService.deleteIndex(term, "address");
		return "delete address index success";
	}
	
	@Remote(url = "delpicindex")
	public String deletePicIndex(Term term) {
		searcherService.deleteIndex(term, "pic");
		return "delete pic index success";
	}
	
	@Remote(url = "batchinsertaddindex")
	public String batchInsertIndex(List list) {
		searcherService.batchaddIndex(list);
		return "batch add index success";
	}
	
}
